//
//  NetworkTool.m
//
//
//  Created by admin on 17/3/2.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "NetworkTool.h"
#import "AFNetworking.h"

@implementation NetworkTool
//{
//    AFHTTPSessionManager *_manager;
//}
+ (instancetype)sharedInstance {
    
    static NetworkTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 子类调用父类的实例化的方法
        instance = [self new];
    });
    
    return instance;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://ope.meigongtech.com/interface/action/upload"] sessionConfiguration:config];
        // 2.1 允许无效证书
        _manager.securityPolicy.allowInvalidCertificates = YES;
        // 2.2 不验证域名
        _manager.securityPolicy.validatesDomainName = NO;
        // 2.3 增加AFN支持的文件类型
        
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", @"text/html",nil];
        // 2.4 让AFN返回原始的二进制数据
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];//格式和上面的一致
        //        _manager.requestSerializer = [AFJSONRequestSerializer serializer];//json格式
        _manager.requestSerializer.timeoutInterval = 5.0;
        
    }
    return self;
}

- (void)setHeaderForAppid:(NSString *)appid appsecret:(NSString *)appsecret{
    if (appid.length > 0 && appsecret > 0) {
        [_manager.requestSerializer setValue:appid forHTTPHeaderField:@"appid"];
        [_manager.requestSerializer setValue:appsecret forHTTPHeaderField:@"appsecret"];
    }
}

#pragma mark - 老的api  接口
- (void)GETWithUrlString:(NSString *)urlString parameters:(id)parameters success:(void(^)(NSDictionary *dict))successBlock failure:(void(^)(NSError *error))failureBlock
{
    [_manager GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 把AFN获取的responseObject,回调给外界
        if (successBlock) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
            successBlock(dict);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}


#pragma mark - 老的api  接口
- (void)POSTWithUrlString:(NSString *)urlString parameters:(id)parameters success:(void(^)(NSDictionary *dict))successBlock failure:(void(^)(NSError *error))failureBlock
{
    [_manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 把AFN获取的responseObject,回调给外界
        if (successBlock) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
            successBlock(dict);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}


- (NSString *)upLoadUrl{
    if (_upLoadUrl == nil) {
        _upLoadUrl = @"http://ope.meigongtech.com/interface/action/upload";
    }
    return _upLoadUrl;
}

@end
